package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiRespModel;

import io.swagger.annotations.ApiOperation;

/**
 * 教师课前准备-下载授课包-轮询下载状态接口
 * 
 */
@CrossOrigin
@RestController
public class LecturePrePackPolledController extends YxyApiBaseController {

    @Value("${yw.esbag.pack.tmp.dir}")
    private String userHome;

    @ApiOperation(value = "轮询下载状态接口")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/prepare/download/polled", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, RespModel respModel,
            @RequestParam String userId, @RequestParam String packId, @RequestParam String dataStr) throws Exception {
        // 文件状态存放位置
        String statusFilePath = getUserHome() + "/" + ".YxYun-YxYunApiTplForYw" + "/" + dataStr + "/download/pack/"
                + userId + "/" + packId + "/" + "status";
        File statusFile = new File(statusFilePath);
        String[] split = statusFile.listFiles()[0].getName().split("_");
        // 下载的文件存放路径
        String packFilePath = getUserHome() + "/" + ".YxYun-YxYunApiTplForYw" + "/" + dataStr + "/download/pack/"
                + userId + "/" + packId + "/" + "pack";

        File packFile = new File(packFilePath);
        // return
        respModel.status = this.getStatus(Integer.parseInt(split[0]));
        respModel.downNumber = this.getFileSize(packFile);
        respModel.zipNumber = Long.parseLong(split[2]);
        respModel.number = Long.parseLong(split[3]);
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 下载状态 0 下载失败，1 下载中 ，2 打包完成
        public int status;
        // 下载大小
        public Long downNumber = 0l;
        // 打包大小
        public Long zipNumber = 0l;
        // 总大小
        public Long number = 0l;
    }

    public int getStatus(int status) {
        // 0-失败, 1-下载中, 2下载完成, 3打包中, 4打包完成
        if (status == 0) {
            return 0;
        } else if (status == 1 || status == 2 || status == 3) {
            return 1;
        } else if (status == 4) {
            return 2;
        } else {
            return 0;
        }
    }

    // 递归 - 取得文件夹大小
    private long getFileSize(File f) throws Exception//
    {
        long size = 0;
        File flist[] = f.listFiles();
        for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
                size = size + getFileSize(flist[i]);
            } else {
                size = size + flist[i].length();
            }
        }
        return size;
    }

    public String getUserHome() {
        return userHome;
    }

    public void setUserHome(String userHome) {
        this.userHome = userHome;
    }
}
